package club.zbiao.modules.system.service.spec;

import club.zbiao.modules.system.domain.Dict;
import club.zbiao.modules.system.domain.DictDetail;
import club.zbiao.modules.system.domain.Role;
import club.zbiao.modules.system.service.dto.DictDetailDTO;
import club.zbiao.modules.system.service.dto.RoleDTO;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhb on 2019/5/23.
 */
@Component
public class DictDetailSpec implements BaseSpec<DictDetailDTO, DictDetail> {

    private DictDetailDTO dictDetail;

    public void init(DictDetailDTO dictDetail){
        this.dictDetail = dictDetail;
    }

    @Override
    public Predicate toPredicate(Root<DictDetail> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {

        List<Predicate> list = new ArrayList<Predicate>();

        Join<Dict,DictDetail> join = root.join("dict",JoinType.LEFT);

        if(!ObjectUtils.isEmpty(dictDetail.getLabel())){
            /**
             * 模糊
             */
            list.add(cb.like(root.get("label").as(String.class),"%"+dictDetail.getLabel()+"%"));
        }

        if(!ObjectUtils.isEmpty(dictDetail.getDictName())){
            list.add(cb.equal(join.get("name").as(String.class),dictDetail.getDictName()));
        }
        Predicate[] p = new Predicate[list.size()];
        return cb.and(list.toArray(p));
    }
}
